#define _CRT_SECURE_NO_WARNINGS 1
#include"CircularQueue.h"


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj = malloc(sizeof(MyCircularQueue));
    obj->a = malloc(sizeof(int) * (k + 1));
    obj->front = obj->back = 0;
    obj->capcity = k;
    return obj;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    if (obj->front == obj->back)
        return true;
    else
        return false;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    if ((obj->back + 1) % (obj->capcity + 1) == obj->front)
        return true;
    else
        return false;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj)) {
        return false;
    }
    obj->a[obj->back] = value;
    obj->back = (obj->back + 1) % (obj->capcity + 1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (obj->front == obj->back) {
        return false;
    }
    else {
        obj->front = (obj->front + 1) % (obj->capcity + 1);
        return true;
    }
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return -1;
    if (obj->back == 0)
        return obj->a[obj->capcity];
    return obj->a[obj->back - 1];
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);
}

